package  
{
	import flash.display.Sprite;
	import flash.text.TextField;
	
	/**
	 * ...
	 * @author patsup
	 * 
	 * 		These class instantiations are held in a vector in DiceArea.
	 * 
	 * p.s. you could possibly have a 100-sided die if you wanted! (see PGame class for relevant constant)
	 * 
	 */
	public class DieObject extends Sprite 
	{
		private var diceSize:Number;		//max dots on a die (max value, e.g. most dice have 6)
		private var dieValue:int = 0;		//current value of the die
		
		//temporary die image parameters
		private const DIE_COLOR:int = 0xC0C0C0;
		private var numLabel:TextField;
		private var dieWidth:int;
		private var dieHeight:int;
		
		public function DieObject(_diceSize:int, _dieWidth:int, _dieHeight:int, _x:int, _y:int) 
		{
			diceSize = _diceSize;
			dieWidth = _dieWidth;
			dieHeight = _dieHeight;
			x = _x;
			y = _y;
			
			numLabel = new TextField();		//for the temporary txt label
			
			Initialize();
		}
		
		//Initialize()
		//Initializes the creation of the DiceArea
		private function Initialize():void
		{
			InitImage();
		}
		
		//InitImage()
		//Initializes the image of the DieObject
		private function InitImage():void
		{
			graphics.beginFill(DIE_COLOR);
			graphics.drawRect(0, 0, dieWidth, dieHeight);
			graphics.endFill();
			
			UpdateImage();
		}
		
		//RollDie()
		//Set the dieValue to a random "roll" and return that value. Also updates the die's image.
		public function RollDie():int
		{
			var _returnValue:int = Math.floor(Math.random() * diceSize) + 1;
			dieValue = _returnValue;
			UpdateImage();
			
			return _returnValue;
		}
		
		//UpdateImage()
		//Update the image the die displays, i.e. when function RollDie() is called
		private function UpdateImage():void
		{
			numLabel.text = String(dieValue);
			numLabel.width = numLabel.textWidth*2;			//reduce the textfield's dimensions so they don't inflate the stage
			numLabel.height = numLabel.textHeight*1.5;
			numLabel.x = 0.5 * dieWidth - 0.5 * numLabel.width;
			numLabel.y = 0.5 * dieHeight - 0.5 * numLabel.height;
			addChild(numLabel);								//now add this txt label to the display list
			
		}
	}

}